package net.adst.ui.mainframe.toppanel;

import net.adst.beans.NaemTec;
import net.adst.beans.OurTecRow;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.ui.mainframe.tabs.AdstTabPanel;
import net.adst.util.DataManager;
import net.adst.util.GBC;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 1:50
 * To change this template use File | Settings | File Templates.
 */
public class CommentArea extends JPanel {

    private JLabel workDescriptionLabel;
    private JTextArea descriptionText;
    private JScrollPane DesciptionScrollPane;

    public CommentArea() {
        setLayout(new GridBagLayout());
        addTextAreas();
    }

    /**
     * Adds to panel description
     */
    private void addTextAreas() {
        workDescriptionLabel = new JLabel("Пометка:");
        descriptionText = new JTextArea() {
            public void setEnabled(boolean enabled) {
                if (enabled)
                    this.setBackground(Color.WHITE);
                else
                    this.setBackground(SystemColor.control);
                super.setEnabled(enabled);
            }
        };
        descriptionText.setLineWrap(true);
        descriptionText.setFont(UIManager.getDefaults().getFont("TabbedPane.font"));
        descriptionText.setEnabled(false);
        DataManager.setObject(VariableNames.COMMENT_TEXT, descriptionText);
        DesciptionScrollPane = new JScrollPane(descriptionText);
        DesciptionScrollPane.setMaximumSize(new Dimension(330, 55));
        DesciptionScrollPane.setPreferredSize(new Dimension(330, 55));
        DesciptionScrollPane.setMinimumSize(new Dimension(330, 55));
        add(workDescriptionLabel, new GBC(0, 0).setInsets(1, 5, 3, 0).setAnchor(GBC.WEST));
        add(DesciptionScrollPane, new GBC(0, 1).setInsets(0, 5, 11, 5).setWeight(1, 1).setFill(GBC.BOTH));
        descriptionText.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                descriptionText.setForeground(Color.BLACK);
            }

            @Override
            public void focusLost(FocusEvent e) {
            }
        });
        descriptionText.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                JButton editButton = (JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_SAVE_BUTTON);
                JTable table;
                if (AdstTabPanel.getIndex() == 0) {
                    table = (JTable) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_TABLE);
                    ArrayList<NaemTec> tecs = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_USING_ROWS);
                    NaemTec tec = tecs.get(table.getSelectedRow());
//                    Integer tecId = Integer.valueOf(table.getValueAt(table.getSelectedRow(), 20).toString());
//                    for (NaemTec item : tecs) {
//                        if (item.getRowInFile() == tecId) {
//                            tec = item;
//                            break;
//                        }
//
//                    }
                    if (tec.getComment().equals(descriptionText.getText())) {
                        editButton.setEnabled(false);
                    } else {
                        editButton.setEnabled(true);
                    }
                    tec.setComment(descriptionText.getText());
                    if (!descriptionText.getText().isEmpty()) {
                        table.setValueAt("*", table.getSelectedRow(), Constants.NAEM_НEADER.length - 1);
                    } else {
                        table.setValueAt("", table.getSelectedRow(), Constants.NAEM_НEADER.length - 1);
                    }


                } else {
                    table = (JTable) DataManager.getObject(VariableNames.UI_OUR_DETAILED_TABLE);
                    ArrayList<OurTecRow> rows = (ArrayList<OurTecRow>) DataManager.getObject(VariableNames.TEC_OUR_USING_ROWS);
                    rows.get(table.getSelectedRow()).setComment(descriptionText.getText());
                    if (rows.get(table.getSelectedRow()).getComment().equals(descriptionText.getText())) {
                        editButton.setEnabled(false);
                    } else {
                        editButton.setEnabled(true);
                    }
                    if (!descriptionText.getText().isEmpty()) {
                        table.setValueAt("*", table.getSelectedRow(), Constants.OUR_НEADER.length - 1);
                    } else {
                        table.setValueAt("", table.getSelectedRow(), Constants.OUR_НEADER.length - 1);
                    }

                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                JButton editButton = (JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_SAVE_BUTTON);
                JTable table;
                if (AdstTabPanel.getIndex() == 0) {
                    table = (JTable) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_TABLE);
                    if (table.getSelectedRow() > -1) {
                        ArrayList<NaemTec> tecs = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_USING_ROWS);
                        NaemTec tec = tecs.get(table.getSelectedRow());
//                        Integer tecId = Integer.valueOf(table.getValueAt(table.getSelectedRow(), Constants.NAEM_НEADER.length - 1).toString());
//                        for (NaemTec item : tecs) {
//                            if (item.getRowInFile() == tecId) {
//                                tec = item;
//                                break;
//                            }
//                        }
                        tec.setComment(descriptionText.getText());
                        if (tec.getComment().equals(descriptionText.getText())) {
                            editButton.setEnabled(false);
                        } else {
                            editButton.setEnabled(true);
                        }
                        if (!descriptionText.getText().isEmpty()) {
                            table.setValueAt("*", table.getSelectedRow(), Constants.NAEM_НEADER.length - 1);
                        } else {
                            table.setValueAt("", table.getSelectedRow(), Constants.NAEM_НEADER.length - 1);
                        }
                    }
                } else {
                    table = (JTable) DataManager.getObject(VariableNames.UI_OUR_DETAILED_TABLE);
                    if (table.getSelectedRow() > -1) {
                        ArrayList<OurTecRow> rows = (ArrayList<OurTecRow>) DataManager.getObject(VariableNames.TEC_OUR_USING_ROWS);
                        rows.get(table.getSelectedRow()).setComment(descriptionText.getText());
                        if (rows.get(table.getSelectedRow()).getComment().equals(descriptionText.getText())) {
                            editButton.setEnabled(false);
                        } else {
                            editButton.setEnabled(true);
                        }
                        if (!descriptionText.getText().isEmpty()) {
                            table.setValueAt("*", table.getSelectedRow(), Constants.OUR_НEADER.length - 1);
                        } else {
                            table.setValueAt("", table.getSelectedRow(), Constants.OUR_НEADER.length - 1);
                        }

                    }
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }

        });
    }
}
